package com.sihan.framework.etm.controller;

import com.alibaba.fastjson.JSONObject;
import com.framework.annotation.BusinessLog;
import com.framework.annotation.ParamParser;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.websocket.WebSocketUtil;
import com.sihan.framework.etm.entity.PushModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@Api(tags = "手动消息推送")
@RestController
@RequestMapping("/webSocket/push")
public class WebSocketController {

    @Autowired
    private WebSocketUtil util;

    //推送数据接口

    @ApiOperation("推送数据")
    @PostMapping("/msg")
    @BusinessLog("推送数据")
    public ApiResult<?> pushToWeb(@RequestBody @ParamParser PushModel pushModel) {
        JSONObject obj = new JSONObject();
        obj.put("date",new Date());
        obj.put("msg",pushModel.getMsg());
        obj.put("deviceCode",pushModel.getDeviceCode());
        obj.put("deviceName",pushModel.getDeviceName());
        //全体发送
        util.sendMsg(obj.toJSONString(),pushModel.getMsgId());
        //单个用户发送 (userId为用户id)
        //webSocket.sendOneMessage(s, obj.toJSONString());
        //多个用户发送 (userIds为多个用户id，逗号‘,’分隔)
        //webSocket.sendMoreMessage(s, obj.toJSONString());
        return ApiResult.success();
    }


}
